/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.db.jpa;

import com.db.jpa.exceptions.NonexistentEntityException;
import com.db.jpa.exceptions.PreexistingEntityException;
import db.Results;
import db.ResultsPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import db.Label;
import db.Word;

/**
 *
 * @author devashish
 */
public class ResultsJpaController {

    public ResultsJpaController() {
        emf = Persistence.createEntityManagerFactory("ExplorePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Results results) throws PreexistingEntityException, Exception {
        if (results.getResultsPK() == null) {
            results.setResultsPK(new ResultsPK());
        }
        results.getResultsPK().setLabelId(results.getLabel1().getLabelId());
        results.getResultsPK().setWordId(results.getWord1().getWordId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Label label = results.getLabel();
            if (label != null) {
                label = em.getReference(label.getClass(), label.getLabelId());
                results.setLabel(label);
            }
            Word word = results.getWord();
            if (word != null) {
                word = em.getReference(word.getClass(), word.getWordId());
                results.setWord(word);
            }
            Label label1 = results.getLabel1();
            if (label1 != null) {
                label1 = em.getReference(label1.getClass(), label1.getLabelId());
                results.setLabel1(label1);
            }
            Word word1 = results.getWord1();
            if (word1 != null) {
                word1 = em.getReference(word1.getClass(), word1.getWordId());
                results.setWord1(word1);
            }
            em.persist(results);
            if (label != null) {
                label.getResultsCollection().add(results);
                label = em.merge(label);
            }
            if (word != null) {
                word.getResultsCollection().add(results);
                word = em.merge(word);
            }
            if (label1 != null) {
                label1.getResultsCollection().add(results);
                label1 = em.merge(label1);
            }
            if (word1 != null) {
                word1.getResultsCollection().add(results);
                word1 = em.merge(word1);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findResults(results.getResultsPK()) != null) {
                throw new PreexistingEntityException("Results " + results + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Results results) throws NonexistentEntityException, Exception {
        results.getResultsPK().setLabelId(results.getLabel1().getLabelId());
        results.getResultsPK().setWordId(results.getWord1().getWordId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Results persistentResults = em.find(Results.class, results.getResultsPK());
            Label labelOld = persistentResults.getLabel();
            Label labelNew = results.getLabel();
            Word wordOld = persistentResults.getWord();
            Word wordNew = results.getWord();
            Label label1Old = persistentResults.getLabel1();
            Label label1New = results.getLabel1();
            Word word1Old = persistentResults.getWord1();
            Word word1New = results.getWord1();
            if (labelNew != null) {
                labelNew = em.getReference(labelNew.getClass(), labelNew.getLabelId());
                results.setLabel(labelNew);
            }
            if (wordNew != null) {
                wordNew = em.getReference(wordNew.getClass(), wordNew.getWordId());
                results.setWord(wordNew);
            }
            if (label1New != null) {
                label1New = em.getReference(label1New.getClass(), label1New.getLabelId());
                results.setLabel1(label1New);
            }
            if (word1New != null) {
                word1New = em.getReference(word1New.getClass(), word1New.getWordId());
                results.setWord1(word1New);
            }
            results = em.merge(results);
            if (labelOld != null && !labelOld.equals(labelNew)) {
                labelOld.getResultsCollection().remove(results);
                labelOld = em.merge(labelOld);
            }
            if (labelNew != null && !labelNew.equals(labelOld)) {
                labelNew.getResultsCollection().add(results);
                labelNew = em.merge(labelNew);
            }
            if (wordOld != null && !wordOld.equals(wordNew)) {
                wordOld.getResultsCollection().remove(results);
                wordOld = em.merge(wordOld);
            }
            if (wordNew != null && !wordNew.equals(wordOld)) {
                wordNew.getResultsCollection().add(results);
                wordNew = em.merge(wordNew);
            }
            if (label1Old != null && !label1Old.equals(label1New)) {
                label1Old.getResultsCollection().remove(results);
                label1Old = em.merge(label1Old);
            }
            if (label1New != null && !label1New.equals(label1Old)) {
                label1New.getResultsCollection().add(results);
                label1New = em.merge(label1New);
            }
            if (word1Old != null && !word1Old.equals(word1New)) {
                word1Old.getResultsCollection().remove(results);
                word1Old = em.merge(word1Old);
            }
            if (word1New != null && !word1New.equals(word1Old)) {
                word1New.getResultsCollection().add(results);
                word1New = em.merge(word1New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ResultsPK id = results.getResultsPK();
                if (findResults(id) == null) {
                    throw new NonexistentEntityException("The results with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ResultsPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Results results;
            try {
                results = em.getReference(Results.class, id);
                results.getResultsPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The results with id " + id + " no longer exists.", enfe);
            }
            Label label = results.getLabel();
            if (label != null) {
                label.getResultsCollection().remove(results);
                label = em.merge(label);
            }
            Word word = results.getWord();
            if (word != null) {
                word.getResultsCollection().remove(results);
                word = em.merge(word);
            }
            Label label1 = results.getLabel1();
            if (label1 != null) {
                label1.getResultsCollection().remove(results);
                label1 = em.merge(label1);
            }
            Word word1 = results.getWord1();
            if (word1 != null) {
                word1.getResultsCollection().remove(results);
                word1 = em.merge(word1);
            }
            em.remove(results);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Results> findResultsEntities() {
        return findResultsEntities(true, -1, -1);
    }

    public List<Results> findResultsEntities(int maxResults, int firstResult) {
        return findResultsEntities(false, maxResults, firstResult);
    }

    private List<Results> findResultsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Results as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Results findResults(ResultsPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Results.class, id);
        } finally {
            em.close();
        }
    }

    public int getResultsCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Results as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
